/*
 * Copyright (c) 2016. 深圳市华百安智能技术有限公司 版权所有
 */

package cn.yulr.multicamera;

/**
 * Created by cktor on 2016/6/7.
 * <a/>
 * 一些公共处理方法
 */
public class CommonMethods {

    /**
     * 通过 byte 数组，获取其中有效的字符串部分
     *
     * @param faceIdByte byte 数组内容
     * @return 生成的相关字符串信息
     */
    public static String getStringFromByte(byte[] faceIdByte) {
        return getStringFromByte(faceIdByte, 0, faceIdByte.length);
    }

    /**
     * 通过 byte 数组，获取其中有效的字符串部分
     *
     * @param faceIdByte byte 数组内容
     * @param startIndex 开始 index
     * @param length     要转换的长度
     * @return 生成的相关字符串信息
     */
    public static String getStringFromByte(byte[] faceIdByte, int startIndex, int length) {
        int index = startIndex;
        for (; index < length; index++) {
            if (faceIdByte[index] == (byte) 0) {
                break;
            }
        }
        byte[] faceIdContent = new byte[index - startIndex];
        System.arraycopy(faceIdByte, startIndex, faceIdContent, 0, index);
        return new String(faceIdContent);
    }

    /**
     * 判断 字符串 既不为 NULL 同时还包含数据信息
     *
     * @param string 字符串
     * @return true，如果字符串为 非空；反之，false
     */
    public static boolean isStringNotEmpty(String string) {
        return string != null && !"".equals(string.trim());
    }

    /**
     * 按照十六进制打印byte[]数组
     *
     * @param buffer 要打印的byte[]数组
     * @return 按照先后顺序组成的byte[]打印String
     */
    public static String byte2hex(byte[] buffer) {
        return byte2hex(buffer, buffer.length);
    }

    public static String byte2hex(byte[] buffer, int length) {
        if (buffer != null && length > 0) {
            StringBuilder stringBuffer = new StringBuilder();
            int readResult = buffer.length > length ? length : buffer.length;
            for (int i = 0; i < readResult; i++) {
                String temp = Integer.toHexString(buffer[i] & 0xFF).toUpperCase();
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                temp = "0x" + temp;
                stringBuffer.append(" ").append(temp);
            }
            return stringBuffer.toString();
        }
        return null;
    }

    /**
     * 按照十六进制打印byte[]数组
     *
     * @param buffer 要打印的byte[]数组
     * @return 按照先后顺序组成的byte[]打印String
     */
    public static String byte2hexNum(byte[] buffer, String splite) {
        if (buffer != null) {
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < buffer.length; i++) {
                String temp = Integer.toHexString(buffer[i] & 0xFF).toUpperCase();
                if (temp.length() == 1) {
                    temp = "0" + temp;
                    temp += splite;
                }
                stringBuffer.append(temp);
            }
            return stringBuffer.toString();
        }
        return null;
    }

    public static String byte2hexNum(byte[] buffer) {
        return byte2hexNum(buffer, "");
    }

    /**
     * 获取byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @param split  数字间的分隔符
     * @return 生成的打印序列
     */
    public static String byte2String(byte[] buffer, String split) {
        StringBuffer stringBuffer = new StringBuffer();
        if (split == null) {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(buffer[i]);
                if (i != buffer.length - 1) {
                    stringBuffer.append(split);
                }
            }
        } else {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(buffer[i]);
                if (i != buffer.length - 1) {
                    stringBuffer.append(" ");
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取无符号byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @param split  数字间的分隔符
     * @return 生成的打印序列
     */
    public static String unsignedByte2String(byte[] buffer, String split) {
        StringBuffer stringBuffer = new StringBuffer();
        if (split != null) {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(unsignedByteToInt(buffer[i]));
                if (i != buffer.length - 1) {
                    stringBuffer.append(split);
                }
            }
        } else {
            for (int i = 0; i < buffer.length; i++) {
                stringBuffer.append(unsignedByteToInt(buffer[i]));
                if (i != buffer.length - 1) {
                    stringBuffer.append(" ");
                }
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取无符号byte[]的打印序列
     *
     * @param buffer 要打印的Byte数组
     * @return 生成的打印序列
     */
    public static String unsignedByte2String(byte[] buffer) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < buffer.length; i++) {
            stringBuffer.append(unsignedByteToInt(buffer[i]));
            if (i != buffer.length - 1) {
                stringBuffer.append(" ");
            }
        }
        return stringBuffer.toString();
    }

    /**
     * int型IP地址转换成byte[]型IP地址
     * 小端模式
     *
     * @param ipInt
     * @return byte[]
     */
    public static byte[] intToBytes(int ipInt) {
        byte[] ipAddr = new byte[4];
        ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF);
        ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF);
        ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF);
        ipAddr[3] = (byte) (ipInt & 0xFF);
        return ipAddr;
    }

    public static byte[] intToBytes(long ipInt) {
        byte[] ipAddr = new byte[4];
        ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF);
        ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF);
        ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF);
        ipAddr[3] = (byte) (ipInt & 0xFF);
        return ipAddr;
    }

    /**
     * int型IP地址转换成byte[]型IP地址
     * 大端模式
     *
     * @param ipInt
     * @return byte[]
     */
    public static byte[] intToBytesBig(int ipInt) {
        byte[] ipAddr = new byte[4];
        ipAddr[3] = (byte) ((ipInt >>> 24) & 0xFF);
        ipAddr[2] = (byte) ((ipInt >>> 16) & 0xFF);
        ipAddr[1] = (byte) ((ipInt >>> 8) & 0xFF);
        ipAddr[0] = (byte) (ipInt & 0xFF);
        return ipAddr;
    }

    public static byte[] shortToByte(short shortValue) {
        byte[] returnValue = new byte[2];
        returnValue[0] = (byte) (shortValue & 0xFF);
        returnValue[1] = (byte) ((shortValue >>> 8) & 0xFF);
        return returnValue;
    }

    public static byte[] shortToByteBig(short shortValue) {
        byte[] returnValue = new byte[2];
        returnValue[1] = (byte) (shortValue & 0xFF);
        returnValue[0] = (byte) ((shortValue >>> 8) & 0xFF);
        return returnValue;
    }

    /**
     * 将长度为2的byte数组转换为16位int
     *
     * @param bytes
     * @return int
     */
    public static short byteArrayToShortBig(byte[] bytes) {
        return (short) ((bytes[1] << 8) | (0xff & bytes[0]));
    }

    /**
     * 将长度为2的byte数组转换为16位int
     *
     * @param bytes
     * @return int
     */
    public static short byteToShort(byte[] bytes) {
        return (short) ((bytes[0] << 8) | (0xff & bytes[1]));
    }

    /**
     * 将长度为2的byte数组转换为16位int
     *
     * @param bytes
     * @return int
     */
    public static short byteToShortBig(byte[] bytes) {
        return (short) ((bytes[1] << 8) | (0xff & bytes[0]));
    }

    /**
     * 返回无符号的Short
     */
    public static int unsignedShortToInt(short s) {
        return (int) s & 0xFFFF;
    }

    /**
     * 获取无符号的byte打印，实际是转换成了Int数据
     *
     * @param b
     * @return
     */
    public static int unsignedByteToInt(byte b) {
        return (int) b & 0xFF;
    }

    /**
     * 将Byte数组数据转化成Int型数据
     * 小端模式
     *
     * @param intByteArray 要转换的Byte数组，长度至少为4位
     * @return 0，如果长度小于或者大于4位；其他，转换后的数据
     */
    public static int byteToInt(byte... intByteArray) {
        int returnValue = 0;
        if (intByteArray != null) {
            // 由高位到低位
            for (int i = 0; i < 4; i++) {
                int shift = (4 - 1 - i) * 8;
                returnValue += (intByteArray[i] & 0x000000FF) << shift;// 往高位游
            }
        }
        return returnValue;
    }

    public static long byteToLong(byte... intByteArray) {
        long returnValue = 0L;
        if (intByteArray != null) {
            // 由高位到低位
            for (int i = 0; i < 4; i++) {
                int shift = (4 - 1 - i) * 8;
                returnValue += (intByteArray[i] & 0x00000000000000FFL) << shift;// 往高位游
            }
        }
        return returnValue;
    }

    /**
     * 将Byte数组数据转化成Int型数据
     * 大端模式
     *
     * @param intByteArray 要转换的Byte数组，长度至少为4位
     * @return 0，如果长度小于或者大于4位；其他，转换后的数据
     */
    public static int byteToIntBig(byte... intByteArray) {
        int returnValue = 0;
        if (intByteArray != null) {
            // 由高位到低位
            for (int i = 0; i < 4; i++) {
                int shift = (i) * 8;
                returnValue += (intByteArray[i] & 0x000000FF) << shift;// 往高位游
            }
        }
        return returnValue;
    }

}
